<?php


namespace app\service\config;


use app\model\config\ConfigModel;
use app\service\BaseService;
use think\facade\Db;

class ConfigService extends BaseService
{
    static $explodeField = ['location_buy_days',];

    public function __construct()
    {
        $this->model = new ConfigModel();
    }

    private static $instance;

    public static function getInstance()
    {
        if (!self::$instance) {
            self::$instance = new ConfigModel();
        }
        return self::$instance;
    }

    /**
     * 添加配置
     * @param $key
     * @param $value
     * @return ConfigModel
     * @throws \Exception
     */
    public static function setConfigValue($key, $value)
    {
        try {
            if (is_array($value)) {
                $value = json_encode($value);
            }
            $val = self::getInstance()->where('config_key', $key)->update(['config_content' => $value]);
            return $val;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 获取一个配置信息
     * @param $key
     * @return array|mixed
     * @throws \Exception
     */
    public static function getConfigValue($key, $default = '')
    {
        try {
            $value = self::getInstance()->where('config_key', $key)->value('config_content', $default);
            if (in_array($key, self::$explodeField)) {
                $value = explode(',', $value);
            } else {
                $json = json_decode($value, true);
                $value = json_last_error() === JSON_ERROR_NONE ? $json : $value;
            }
            return $value;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 获取几个配置信息
     * @param array $keyArr
     * @return array
     * @throws \Exception
     */
    public static function getConfigValueArray(array $keyArr)
    {
        try {
            $arr = self::getInstance()->whereIn('config_key', $keyArr)->column('config_content', 'config_key');
            $list = [];
            foreach ($arr as $k => $value) {
                if (in_array($k, self::$explodeField)) {
                    $list[$k] = explode(',', $value);
                } else {
                    $json = json_decode($value, true);
                    $value = json_last_error() === JSON_ERROR_NONE ? $json : $value;
                    $list[$k] = $value;
                }
            }
            return $list;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }

    /**
     * 取某一端所有配置
     * @param $type
     * @return array
     * @throws \Exception
     */
    public static function getTypeConfigValueArray($type)
    {
        try {
            $arr = self::getInstance()->where('status', 0)->where('type', $type)->column('config_content', 'config_key');
            $list = [];
            foreach ($arr as $k => $value) {
                if (in_array($k, self::$explodeField)) {
                    $list[$k] = explode(',', $value);
                } else {
                    $json = json_decode($value, true);
                    $value = json_last_error() === JSON_ERROR_NONE ? $json : $value;
                    $list[$k] = $value;
                }
            }
            return $list;
        } catch (\Exception $exception) {
            throw new \Exception($exception->getMessage());
        }
    }
}